(function (doc, win) {
    var docEl = doc.documentElement,
        resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
        recalc = function () {
            var clientWidth = docEl.clientWidth;
            if (!clientWidth) return;
            var num = 20 * (clientWidth / 320);
            if (num >= 20 && num <= 40) {
                docEl.style.fontSize = num + 'px';
            } else if (num < 20) {
                docEl.style.fontSize = '20px';
            } else if (num > 40) {
                docEl.style.fontSize = '40px';
            }
        };
        
    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

(function () {
    // var toTop = document.querySelector('.totop');
    var toTop = document.createElement('div');
    toTop.setAttribute('class', 'totop');
    toTop.innerHTML = '<img src="/Template/mobile/new/Static/images/totop.png">';
    document.body.appendChild(toTop);
    
    toTop.addEventListener('touchstart', gotop, false);

    window.addEventListener('scroll', function () {
        showHide();
    });
    
    function gotop() {
        var timer = null;
        var bSys = true;

        window.addEventListener('scroll', function () {
            if(!bSys) {clearInterval(timer);}
            bSys = false;
        }, false);

        clearInterval(timer);
        timer = setInterval(function () {

            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            // var iSpeed = Math.floor(-scrollTop / 8);
            var iSpeed = -80;
            
            if(scrollTop == 0) {
                clearInterval(timer);
            }
            
            bSys = true;
            document.documentElement.scrollTop = document.body.scrollTop = scrollTop + iSpeed;
        }, 30);
    }

    function showHide() {
        var scorllTop = document.documentElement.scrollTop || document.body.scrollTop;
        if (scorllTop > 200) {
            toTop.style.display = 'block';
        } else {
            toTop.style.display = 'none';
        }
    }
})();